var category = "";                  // 初始类别为空


$(function () {
    $.ajax({
        url: Feng.ctxPath + "/consultant/getDoctor",
        type: "get",
        success: function (data) {
            var txt = '<option value="未指定医生">未指定医生</option>';
            for (var i = 0; i < data.length; i++) {
                txt += '<option value="' + data[i].doctor + '">' + data[i].doctor + '</option>';
            }
            document.getElementById('doctor').innerHTML = txt;
            //状态单选
            var seleteDoctorValue = $('#doctorValue').val();
            if (seleteDoctorValue != "") {
                $("#doctor option[value=" + seleteDoctorValue + "]").attr("selected", "selected");
            }
        },
        error: function (data) {
            alert('内部故障!');
        }
    });
    //初始化下拉TC列表
    $.post(Feng.ctxPath + "/tc/getLists", function (data) {
        var txt = '<option value="">--请选择现场TC--</option>';
        for (var i = 0; i < data.length; i++) {
            txt += '<option value="' + data[i].tcName + '">' + data[i].tcName + '</option>';
        }
        document.getElementById('fieldTc').innerHTML = txt;
        //状态单选
        var seletetcName = $('#tcNameValue').val();
        if (seletetcName != "") {
            $("#fieldTc option[value=" + seletetcName + "]").attr("selected", "selected");
        }
    }, "json");
  /*  //初始化下拉预约项目列表
    $.post(Feng.ctxPath + "/payService/getList", function (data) {
        var txt = '';
        data.forEach(function (key, value) {
            txt += '<div class="col-sm-3"><input type="checkbox" name="about" value="' + key.department + '" id="click_' + value + '"><label for="click_' + value + '" style="font-weight: 500;margin-left: 10px;">' + key.department + '</label></div>'
        });
        document.getElementById('projectData').innerHTML = txt;
    }, "json");
*/

    // $("#diagnosis").click();


    var start = $("#startTimeValue").val();
    var end = $("#endTimeValue").val();
    var startOne = start.substring(0, 19);
    var endOne = end.substring(0, 19)
    var startTwo = new Date(startOne);
    var endTwo = new Date(endOne);
    var continued = parseInt(endTwo - startTwo) / 1000 / 60;
    $("#duration").val(continued)

});

/*
//初诊
$("#diagnosis").click(function () {
    category = "diagnosis";
    $(this).css({'background': '#31add1','color': 'white'});
    $("#revisit").css({'background': 'white','color': '#333333'});
});
//复诊
$("#revisit").click(function () {
    category = "revisit";
    $(this).css({'background': '#31add1','color': 'white'});
    $("#diagnosis").css({'background': 'white','color': '#333333'});
});
*/


//持续时间操作
function continued(obj) {
    var time = $("#duration").val();    //获取持续时间
    if (obj == "-") {
        if (time == 0) {
            $("#duration").val(0);
        } else {
            $("#duration").val(parseInt(time) - 15);
        }
    } else if (obj == "+") {
        $("#duration").val(parseInt(time) + 15);
    }
}

//新建患者
function addToReservations() {
    var index = layer.open({
        type: 2,
        title: '添加到店客户',
        area: ['100%', '100%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/customer/to_nature_courtyard_adds'
    });
    this.layerIndex = index;
}

//提交
function submission() {
    var startTimeValue = document.getElementById("startTimeValue").value;
    var endTimeValue = document.getElementById("endTimeValue").value;
    var customerPhone = document.getElementById("customerPhone").innerHTML;
    var fieldTc = document.getElementById("fieldTc").value;
    var doctor = document.getElementById("doctor").value;
    var remarks = document.getElementById("remarks").value;
    var reservationsId = document.getElementById("reservationsId").value;
    var customerId = document.getElementById("customerId").value;
    var reach = document.getElementById("reach").value;
    var check = $("input[type='checkbox']:checked");
    var startTime = document.getElementById("startTime").value;
    var duration = document.getElementById("duration").value;

    var projectData = "";
    for (var i = 0; i < check.length; i++) {
        projectData += $(check[i]).val() + ",";
    }
    /*  var customerStatus="";
      if( category == "diagnosis"){  //初诊
          customerStatus="新客首次";
          $.ajax({
              url: Feng.ctxPath +"/customer/SaveReservation",
              type: "post",
              data: {
                  "reservationsId": reservationsId,
                  "endTimeValue":endTimeValue,
                  "startTimeValue":startTimeValue,
                  "customerStatus":customerStatus,
                  "customerPhone": customerPhone,
                  "customerId": customerId,
                  "projectData": projectData,
                  "fieldTc": fieldTc,
                  "doctor": doctor,
                  "remarks": remarks
              },
              success: function (data) {
                  alert('预约成功!');
                  window.parent.$('#calendar').fullCalendar( 'refetchEvents' );
                  parent.layer.close(parent.layer.getFrameIndex(window.name))
              },
              error: function (data) {
                  alert('预约失败!');
              }
          });
      }else {
          customerStatus = "复诊";*/
   if(reach == "" || reach == null){
       alert("患者未输入！")
   }else {
       $.ajax({
           url: Feng.ctxPath + "/customer/addCustomerFz",
           type: "post",
           data: {
               "reservationsId": reservationsId,
               // "endTimeValue": endTimeValue,
               "startTimeValue": startTime,
               "customerPhone": customerPhone,
               "customerId": customerId,
               "projectData": projectData,
               "fieldTc": fieldTc,
               "doctor": doctor,
               "remarks": remarks,
               "duration":duration
           },
           success: function (data) {
               alert('预约成功!');
               window.parent.$('#calendar').fullCalendar('refetchEvents');
               parent.layer.close(parent.layer.getFrameIndex(window.name))
           },
           error: function (data) {
               alert('预约失败!');
           }
       });
   }
    //}
}


//查询
function search() {
    var reach = document.getElementById("reach").value;
    if(reach != ""){
        $.ajax({
            url: Feng.ctxPath + "/customer/getCustomerByReach/" + reach,
            type: "post",
            success: function (data) {
                if (data == "" || data == null) {
                    alert('未搜索到该用户!');
                } else {
                    $("#dataBox").show();
                    var patient = "";
                    for (var i = 0; i < data.length; i++) {
                        patient += '<li style="padding: 10px 0 10px 10px;border-bottom: 1px solid #cccccc" id="' + data[i].id + '" class="' + data[i].id + '">' +
                            '<p><span>' + data[i].customerName + '</span>&nbsp;&nbsp;<span>' + data[i].customerAge + '</span>岁</p>' +
                            // '<p><span>'+data[i].medicalRecordNumber+'</span>&nbsp;&nbsp;<span>'+data[i].arrivalStore+'</span></p>' +
                            '<p>' + data[i].customerPhone + '</p>' +
                            '<p>' + data[i].sex + '</p>' +
                            // '<p>'+data[i].firstVisitDate+'</p>' +
                            '</li>'
                    }
                    $("#dataUl").html(patient);
                }
            },
            error: function (data) {
                alert('内部故障!');
            }
        });
    }

}


//点击显示
$("#dataUl").on('click', 'li', function () {
    var reservationsId = $(this).attr('class');
    $.ajax({
        url: Feng.ctxPath + "/customer/getCustomerByReservationsId/" + reservationsId,
        type: "post",
        success: function (data) {
            // document.getElementById("customerId").value = data[0].id;
            document.getElementById("reservationsId").value = reservationsId;
            document.getElementById("customerName").innerHTML = data[0].customerName;
            document.getElementById("customerAge").innerHTML = data[0].customerAge;
            document.getElementById("customerPhone").innerHTML = data[0].customerPhone;
            if (data[0].customerSex == '1') {
                document.getElementById("customerSex").innerHTML = '男';
            } else if (data[0].customerSex == '2') {
                document.getElementById("customerSex").innerHTML = '女';
            } else {
                document.getElementById("customerSex").innerHTML = '未知';
            }
            document.getElementById("firstVisitDate").innerHTML = data[0].firstVisitDate;
            // document.getElementById("province").innerHTML = data[0].province;
            // document.getElementById("cities").innerHTML = data[0].cities;
            // document.getElementById("region").innerHTML = data[0].region;
            // document.getElementById("medicalRecordNumber").innerHTML = data[0].medicalRecordNumber;
            // document.getElementById("firstTc").innerHTML = data[0].tcName;
            document.getElementById("reach").value = data[0].customerName;
            $("#dataBox").hide();
        }
    });
});

//修改患者信息
$("#updateCustomerInfo").click(function () {
    var reservationsId = document.getElementById("reservationsId").value;
    var customerName = document.getElementById("customerName").innerHTML;
    if (customerName == "" || customerName == null) {
        alert("请关联患者!");
    } else {
        var index = layer.open({
            type: 2,
            title: '修改患者信息',
            area: ['100%', '100%'], //宽高
            fix: true, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/customer/to_nature_courtyard_update/' + reservationsId
        });
        this.layerIndex = index;
    }

})